/**
 * 
 */
package uk.co.lunagroup.vladar.oncenter;

import org.apache.poi.hssf.usermodel.HSSFSheet;

/**
 * allows user to choose file (take off) generated from OnCenter and converts this into
 * format usable for doing tenders
 * @param args
 * @author Robert Kowalski
 * @version 0.1
 * 
 */

public class OnScreenSanity {

	

	
	public static void main(String[] args) {

		int columns=10; //number of columns to copy to array
		//TODO in the future add option to choose sheet
		String sheet = "Sheet1"; //name of the sheet where from copy
	
		
		String file= new String();
		file = ChooseFile.pickFile();
		
		//Open excel and return worksheet;
		HSSFSheet worksheet = ReadExcel.openExcelFile(file, sheet, columns); //file location, number of columns and number. rows calculated
		//read data from worksheet and throw this to the array
		String [][] inputArray=ReadExcel.readData(worksheet, columns); //reads all data from sheet (rows calculated) and columns as parameters
		
		convertData(inputArray);
	}
/**
 * Convert data from OnCenter software into usable set of informations
 * @param coreData
 * @return table of strings with converted data
 */

	
	public static String[][] convertData(String [][] coreData) {
		
		
		String[][] outputArray =cleanUp(coreData);
		
		//add missing information for first theree column
		outputArray=addMissingInformations (outputArray);
				
		//Removing the Multi-Are Total
		int columnNumber = 3; //row number where I have MultiTotals to clear
		String textToCheck="Multi-Area Total";
		outputArray=clearMultiTotals(outputArray, columnNumber, textToCheck);
			
		//TODO copy Qty2 and UOM2. have to duplicate output array first because it is holding now proper informations to copy
		//temporary array to store what we did so f		
		String[][] tempArray=outputArray;  // alternatively can be used ArrayOperations.cloneArray(outputArray);
		
		
		//adding Qty2, UOM2, -1, -2 marks
		outputArray=copyQty2AndUOM2(outputArray, tempArray);
		
		//debug
		ArrayOperations.printArray(outputArray);
		//endof debug
		
	
		return outputArray;
		
	}
	
	
/**
 * deletes all rows from array where qty1 = null
 */
	public static String[][] cleanUp (String [][] array){
		int YSize = ArrayOperations.checkArrayYSize(array);
		int qty1Column =4;
		
		for (int i=0; i<YSize;i++) {
			
			if (array[qty1Column][i].length()==0) {
				array=ArrayOperations.deleteRow(array, i);
				//new array is smaller by one row - so resize array and change counter
				//array=ArrayOperations.shrinkArray(array, 0, 1);
				YSize=YSize-1;
				i=i-1;
			}
		}
		;
		return array;
	}
	
	
	/**
	 * This method clear unnecesarry MultiTotals
	 * @param inputArray
	 * @return
	 */
	public static String[][] clearMultiTotals(String[][] inputArray, int columnNumber, String textToCheck) {
		int rowCount=ArrayOperations.checkArrayYSize(inputArray);
			for (int i=0; i<rowCount; i++) {
				if (textToCheck.equals(inputArray[columnNumber][i])) {
					inputArray=ArrayOperations.deleteRow(inputArray, i);
					rowCount=rowCount-1; //returned table is one row shorter
				}
				
			}
			//debug 
			//System.out.println(Arrays.toString(inputArray));
		return inputArray;
	}
	/**
	 * adding to the condition numbers part -1, -2 etc. (added part). for example condition nr 1 will
	 * be changed to 1-1
	 * @param rowNumber
	 * @param columnNumber
	 * @param inputArray
	 * @param outputArray
	 * @param addedPart
	 * @return
	 */
public static String[][] changeFirstColumn (int rowNumber, int columnNumber,String[][] inputArray, String[][] outputArray, String addedPart) {
	outputArray[columnNumber][rowNumber]=inputArray[columnNumber][rowNumber]+addedPart;
	//debug
	//System.out.println(outputArray[columnNumber][rowNumber]);
	//end debug
	return outputArray;
}

/**
  * checking if the cell is empty. Specific to OnCenter only 
  * some cells looks like empty but in fact they have set of spaces (second parameter)
  * @param cellToTest
  * @return true if empty
  */
public static boolean checkIfEmpty (String cellToTest) {
	boolean result = false;
		//debug
		//System.out.print(cellToTest);
	
	//second parameter - ONCenter is returning some cells which looks like empty, but it is not empty.
		if ("".equals(cellToTest) || "   ".equals(cellToTest) || cellToTest == null) { 
			result=true;
		}
			
	return result;
}

/**
 * adding informations from row above if cell is empty
 * @param inputArray
 * @param outputArray
 * @param inputArrayYSize
 * @param inputArrayXSize
 * @return array of strings with updated data
 */
public static String[][] addMissingInformations (String[][] inputArray) {
	
	int inputArrayYSize=ArrayOperations.checkArrayYSize(inputArray);
	int inputArrayXSize=ArrayOperations.checkArrayXSize(inputArray);
	
	String[][] outputArray=new String[inputArrayXSize][inputArrayYSize];
	
	for (int i=0; i<inputArrayYSize; i++) {
		
		
		//loop to go through columns
			for (int j=0; j<inputArrayXSize; j++) {
				//checking if columns 2 is ampty and 3 NOT EMPTY. REQUIRED NOT TO ADD informations at end of file with empty cells
				//MUST BE HERE
				boolean check = (checkIfEmpty(inputArray[2][i]) && !checkIfEmpty(inputArray[3][i]));					
				if (check==true) {
				
					//easier to read
					int destY=i;
					int destX=j;
					int sourceY=i-1; //because copying from row above
					int sourceX=j;
					int YSize=1;
					int XSize=1;
					outputArray=ArrayOperations.copyCellRange(inputArray, inputArray, destY, destX, sourceY, sourceX, YSize, XSize);
					}
				else {
					//if not copy the same cell
					
					//easier to read
					int destY=i;
					int destX=j;
					int sourceY=i; 
					int sourceX=j;
					int YSize=1;
					int XSize=1;
					outputArray=ArrayOperations.copyCellRange(inputArray, outputArray, destY, destX, sourceY, sourceX, YSize, XSize);
				}
				//debug
				//System.out.print(outputArray[j][i]+" ");
			}
	//debug
	System.out.println();
	
	}
	return outputArray;
}

/**
 * this methods adds missing UOM2 and Qty2
 * @param destArray
 * @param sourceArray
 * @return
 */
public static String[][] copyQty2AndUOM2 (String[][] destArray, String[][] sourceArray){
	
	int extraRows = ArrayOperations.checkArrayYSize(sourceArray); //will extend array by number of rows existing now - worst case scenario
	int extraColumns =0; //do not make array wider
	
	destArray = ArrayOperations.resizeArray(destArray, extraColumns, extraRows);
	
	int qty2column = 6; //Qty2 is in column 6
	int qty1column = 4; //Qty1 column
	int startOffset = 1; //do not want start with row 1 as this is heading
	int YSize = ArrayOperations.checkArrayYSize(sourceArray);
		
	String valueToCheck ="0"; //values which we are looking for
	
	int count=YSize; //where to add in destination array
	
	for (int i=0+startOffset; i<YSize; i++) {
		boolean check = ArrayOperations.compareCell(sourceArray[qty2column][i], valueToCheck); //check if there is "0" in cell
			if (check == false) {
				//first I have to copy row
				destArray=ArrayOperations.copyRow(sourceArray, destArray, count, i);
											
				//now copy qty2 and uom2 in one run
				destArray=ArrayOperations.copyCellRange(destArray, destArray, count, qty1column, i, qty2column, 1, 2);
				
				//adding mark -2 to copied elements
				destArray=changeFirstColumn(count, 0, destArray, destArray, "-2");
					
				count++;
			}
	
	//change first column - add -1
	destArray=changeFirstColumn(i,0,destArray,destArray,"-1");
		
	}
		
	return destArray;
}

}
	